<?php

interface ActionHandler
{

    public function execute();

}

#[Attribute]
class SetUp
{
}

/**
 * Class CopyFile
 */
class CopyFile implements ActionHandler
{
    public string $fileName;
    public string $targetDirectory;

    #[SetUp]
    public function fileExists()
    {
        if (!file_exists($this->fileName)) {
            throw new RuntimeException('文件不存在');
        }
    }

    #[SetUp]
    public function targetDirectoryExists()
    {
        if (!is_dir($this->targetDirectory)):
            mkdir($this->targetDirectory);
        endif;
    }

    public function execute()
    {
        copy($this->fileName, $this->targetDirectory.'/'.basename($this->fileName));
    }
}

function executeAction(ActionHandler $handler)
{
    $reflection = new ReflectionObject($handler);
    foreach ($reflection->getMethods() as $method) {
        $attributes = $method->getAttributes(SetUP::class);
        if (count($attributes) > 0) {
            // 获取到注解所标注的方法的名字
            $methodName = $method->getName();
            // 调用方法
            $handler->$methodName();
        }
    }
    $handler->execute();
}

$copyAction = new CopyFile();
$copyAction->fileName = 'timg.jpg';
$copyAction->targetDirectory = 'C:\Users\zcg\Pictures\aaa';
executeAction($copyAction);
